package com.itheima.sfbx.insurance.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.itheima.sfbx.framework.commons.basic.ResponseResult;
import com.itheima.sfbx.framework.commons.utils.ResponseResultBuild;
import com.itheima.sfbx.insurance.dto.CoefficentVO;
import com.itheima.sfbx.insurance.service.ICoefficentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "系数管理")
@RestController
@RequestMapping("/coefficent")
public class CoefficentController {

    @Autowired
    private ICoefficentService coefficentService;

    /**
     * 分页查询系数项
     *
     * @param coefficentVO 查询参数
     * @param pageNum      页码
     * @param pageSzie     每页条数
     * @return 分页数据
     */
    //属于方法, 使用swagger
    @ApiOperation(value = "根据条件分页查询系数项", notes = "根据分页查询系数项")
    //指定swagger的方法参数有哪一些, 以后在swagger的文档上看到这些参数的具体属性
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coefficentVO", value = "查询参数", required = true, dataType = "CoefficentVO"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageSzie", value = "每页条数", required = true, dataType = "int")
    })
    //ApiOperationSupport 在查询条件对象中指定哪些属性可以展示
    @ApiOperationSupport(ignoreParameters = {"coefficentVO.id", "coefficentVO.createTime", "coefficentVO.updateTime", "coefficentVO.createBy", "coefficentVO.updateBy"})
    @PostMapping("page/{pageNum}/{pageSzie}")
    public ResponseResult<Page<CoefficentVO>> findCoefficentVOPage(@RequestBody CoefficentVO coefficentVO,
                                                                   @PathVariable("pageNum") int pageNum,
                                                                   @PathVariable("pageSzie") int pageSzie) {
        //调用service的方法,进行查询, 并获取结果
        Page<CoefficentVO> page = coefficentService.findPage(coefficentVO, pageNum, pageSzie);
        //封装结果并返回
        return ResponseResultBuild.successBuild(page);
    }

    /**
     * 保存系数项
     *
     * @param coefficentVO 保存参数
     * @return 保存结果
     */

    @ApiOperation(value = "保存系数项", notes = "保存系数项")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coefficentVO", value = "系数项", required = true, dataType = "CoefficentVO")
    })
    @ApiOperationSupport(ignoreParameters = {"coefficentVO.id", "coefficentVO.createTime", "coefficentVO.updateTime", "coefficentVO.createBy", "coefficentVO.updateBy"})
    @PutMapping
    public ResponseResult<CoefficentVO> createCoefficent(@RequestBody CoefficentVO coefficentVO) {
        //调用service的方法,进行保存, 并获取结果
        CoefficentVO result = coefficentService.save(coefficentVO);
        //封装结果并返回
        return ResponseResultBuild.successBuild(result);
    }

    /**
     * 修改系数项
     *
     * @param coefficentVO 修改参数
     * @return 修改结果
     */
    @ApiOperation(value = "修改系数项", notes = "修改系数项")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coefficentVO", value = "系数项", required = true, dataType = "CoefficentVO")
    })
    @ApiOperationSupport(ignoreParameters = {"coefficentVO.id", "coefficentVO.createTime", "coefficentVO.updateTime", "coefficentVO.createBy", "coefficentVO.updateBy"})
    @PatchMapping
    public ResponseResult<Boolean> updateCoefficent(@RequestBody CoefficentVO coefficentVO) {
        //调用service的方法,进行修改, 并获取结果
        Boolean result = coefficentService.update(coefficentVO);
        //封装结果并返回
        return ResponseResultBuild.successBuild(result);
    }

    /**
     * 删除系数项
     *
     * @param coefficentVO 删除参数
     * @return 删除结果
     */
    @ApiOperation(value = "删除系数项", notes = "删除系数项")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coefficentVO", value = "系数项", required = true, dataType = "CoefficentVO")
    })
    @ApiOperationSupport(ignoreParameters = {"coefficentVO.id", "coefficentVO.createTime", "coefficentVO.updateTime", "coefficentVO.createBy", "coefficentVO.updateBy"})
    @DeleteMapping
    public ResponseResult<Boolean> deleteCoefficent(@RequestBody CoefficentVO coefficentVO) {
        //调用service的方法,进行删除, 并获取结果
        Boolean result = coefficentService.delete(coefficentVO.getCheckedIds());
        //封装结果并返回
        return ResponseResultBuild.successBuild(result);
    }

    /***
     * @description 多条件查询系数项列表
     * @param coefficentVO 系数项VO对象
     * @return List<CoefficentVO>
     */
    @PostMapping("list")
    @ApiOperation(value = "多条件查询系数项列表", notes = "多条件查询系数项列表")
    @ApiImplicitParam(name = "coefficentVO", value = "系数项VO对象", required = true, dataType = "CoefficentVO")
    @ApiOperationSupport(includeParameters = {"coefficentVO.dataState", "coefficentVO.coefficentKey", "coefficentVO.coefficentKeyName", "coefficentVO.coefficentVal", "coefficentVO.sortNo", "coefficentVO.remake", "coefficentVO.coefficentType"})
    public ResponseResult<List<CoefficentVO>> coefficentList(@RequestBody CoefficentVO coefficentVO) {
        List<CoefficentVO> coefficentVOList = coefficentService.findList(coefficentVO);
        return ResponseResultBuild.successBuild(coefficentVOList);
    }

    /***
     * @description 系数项key查询CoefficentVO
     * @param coefficentKey 系数项key
     * @return CoefficentVO
     */
    @PostMapping("coefficent-key/{coefficentKey}")
    @ApiOperation(value = "系数项key查询CoefficentVO", notes = "系数项key查询CoefficentVO")
    @ApiImplicitParam(paramType = "path", name = "coefficentKey", value = "系数项key", dataType = "String")
    public ResponseResult<CoefficentVO> findByCoefficentKey(@PathVariable("coefficentKey") String coefficentKey) {
        CoefficentVO coefficentVO = coefficentService.findByCoefficentKey(coefficentKey);
        return ResponseResultBuild.successBuild(coefficentVO);
    }
}


